package menu.lt.pers.menu.dao;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import menu.lt.pers.menu.R;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class SoundUtils {
    private static SoundPool soundPool;
    private static boolean soundSt = true; //音效开关
    private static Context context;
    private static Map<Integer,Integer> soundMap; //音效资源id与加载过后的音源id的映射关系表

    public static void init(Context c) {
        context = c;
        initSound();
    }
    private static void initSound() {
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC,100);
        soundMap = new HashMap<Integer,Integer>();
        soundMap.put(R.raw.bg_music, soundPool.load(context, R.raw.bg_music, 1));
        soundMap.put(R.raw.ding, soundPool.load(context, R.raw.ding, 1));
    }
    public static void playSound(int resId) {
        if (soundSt == false)
            return;
        Integer soundId = soundMap.get(resId);
        if (soundId != null)
            soundPool.play(soundId, 1, 1, 1, 0, 1);
    }
    public static void boom() {
        playSound(R.raw.ding);
    }
}
